package com.eight.cloud.mybatisflex.listener;

import cn.hutool.core.util.ObjectUtil;
import com.eight.cloud.core.global.model.BaseUser;
import com.eight.cloud.core.utils.supply.UserContext;
import com.eight.cloud.mybatisflex.entity.BaseEntity;
import com.mybatisflex.annotation.InsertListener;

import java.time.LocalDateTime;

/**
 * @ClassName: GlobalInsertListener
 * @Author: TXC
 * @Date: 2025-09-15 17:35
 **/
public class GlobalInsertListener implements InsertListener {
    @Override
    public void onInsert(Object entity) {
        BaseEntity baseEntity = (BaseEntity) entity;
        BaseUser loginUser = UserContext.getUser();
        if (ObjectUtil.isNotNull(loginUser)) {
            baseEntity.setCreateBy(loginUser.getUuid());
            baseEntity.setUpdateBy(loginUser.getUuid());
        }
        baseEntity.setCreateTime(LocalDateTime.now());
        baseEntity.setUpdateTime(LocalDateTime.now());
    }
}
